function messageAlert(txt){
	//alert(txt);
	layer.msg(txt);
}

(function ($) {
		$.fn.zhlUploads = function (param, eFun) {
			var zhl_upload_url = typeof param['zhl_upload_url'] != "undefined" && param['zhl_upload_url'] ? param['zhl_upload_url'] : '';
			var zhl_upload_num = typeof param['zhl_upload_num'] != "undefined" && param['zhl_upload_num'] ? parseInt(param['zhl_upload_num']) : 1;
			var zhl_upload_size = typeof param['zhl_upload_size'] != "undefined" && param['zhl_upload_size'] ? parseInt(param['zhl_upload_size']) : 2*1024*1024;
			var zhl_upload_box = typeof param['zhl_upload_box'] != "undefined" && param['zhl_upload_box'] ? param['zhl_upload_box'] : '';
			var zhl_upload_css = typeof param['zhl_upload_css'] != "undefined" && param['zhl_upload_css'] ? param['zhl_upload_css'] : 'zhl_upload_css_one'; 
			
			if(zhl_upload_css == 'zhl_upload_css_one' || zhl_upload_css == 'zhl_upload_css_two'){
				zhl_upload_num = 1; 
			}
			
			var html = '<div id="zhl_upload_mask"></div>';
			html += '<div id="zhl_upload_box" class="'+zhl_upload_css+'">';
			html += '<div id="zhl_upload_padding">';
			html += '<div id="zhl_upload_close">×</div>';
			html += '<input type="file" id="zhl_upload_file" hidefocus   style="display:none" multiple="multiple">';
			html += '<span id="zhl_upload_imgs"></span>';
			html += '<span id="zhl_upload_add" onclick="zhl_upload_file.click()" /></span>';
			html += '<div id="zhl_upload_sub" >上传</div>';
			html += '<span id="zhl_upload_hint">最多传 <b id="zhl_upload_no">'+zhl_upload_num+'</b> 张</span>';
			html += '</div>';
			html += '</div>';
			$('body').prepend(html);
			$(".zhl_upload_css_one #zhl_upload_file").removeAttr('multiple');
			$(".zhl_upload_css_two #zhl_upload_file").removeAttr('multiple');
			
			$("#zhl_upload_file").change(function(e){
				var fileAll = this.files.length;
				for(var i = 0; i < fileAll; i++){
					var file = this.files[i];
					if(file.size >  zhl_upload_size){  
						var d = parseInt(i+1);
						messageAlert("你选择的图片第"+d+"个太大了！");return false;
					}
					if(!/image\/\w+/.test(file.type)){
						messageAlert("文件必须为图片！"); return false;
					}
					var reader = new FileReader();
					reader.onerror = function (e) {
						messageAlert('文件上传异常请关闭重试....');return false;
					}
					reader.onabort = function(e) {
						messageAlert('文件上传异常请关闭重试....');return false;
					};
					reader.onload = function (e) {
						var imgstr='<img src="'+e.target.result+'" class="zhl_upload_tmp"/>';
						var len = $('#zhl_upload_imgs>img').length;
						if(len < zhl_upload_num){
							if(len == zhl_upload_num-1){
								$('#zhl_upload_add').hide();
							}else{
								$('#zhl_upload_add').show();
							}
							$('#zhl_upload_imgs').append(imgstr);
						}else{
							messageAlert('最多'+zhl_upload_num+'张图片');
							$('#zhl_upload_add').hide();return false;
						}
					}
					reader.readAsDataURL(file);
				}
			});
			
			//$("#zhl_upload_imgs img").click(function(){
			$("#zhl_upload_imgs").delegate("img","click",function(){
				if(confirm('删除该张图片？')){
					$(this).remove();
					$('#zhl_upload_add').show();
				}
			});
			
			$("#zhl_upload_close").click(function(){
				zhl_upload_close();
			})
			
			function zhl_upload_close(){
				$("#zhl_upload_mask").remove();
				$("#zhl_upload_box").remove();
			}
			
			$("#zhl_upload_sub").click(function(){
				if(zhl_upload_url == ''){
					messageAlert('上传连接不能为空');return false;
				}
				var base64Data = {};
				$("#zhl_upload_imgs img").each(function(e){
					base64Data[e] = $(this).attr('src');
				});
				if(!judgeEmpty(base64Data)){
					base64_uploading(zhl_upload_url,base64Data,zhl_upload_box, eFun);
				}else{
					messageAlert('请选择文件');return false;
				}
			})
			
			function judgeEmpty(data) {
			  return (Array.isArray(data) && data.length === 0) || (Object.prototype.isPrototypeOf(data) && Object.keys(data).length === 0);
			}

			function base64_uploading(zhl_upload_url,base64Data,zhl_upload_box, eFun){
				// {imgurl: "./update/2018102500422009778.jpeg", code: "10000", msg: "图片上传成功"}
				$.post(zhl_upload_url,{base64Data},function(data){
					console.log(data);
					//var jsonarray= $.parseJSON(data);
					var jsonarray = data;
					//var jsonarray= data;
					if(zhl_upload_box != ''){
						var html = '';
						var msg = '';
						var flag = false;
						for(var i=0; i<jsonarray.length; i++){
							html += jsonarray[i].imgurl+'|';
							msg += (i+1)+':'+jsonarray[i].msg+'。|';
							if(jsonarray[i].code == '10000'){
								flag = true;
							}
						}
						msg = msg.substring(0, msg.length - 1);
						if(flag == true){
							html = html.substring(0, html.length - 1);
							//console.log(html);
							$(zhl_upload_box).val(html);
							$(zhl_upload_box).text(html);

							/////////
							eFun(html);
							///////////////////
							//cms提升体验
							$(zhl_upload_box).parents('.form_item').find('.show_pic_btn').show();
							$(zhl_upload_box).parents('.form_item').find('.del_input_btn').show();
							$(zhl_upload_box).parents('.form_item').find('.show_pic_btn').attr('img_src',http_link+html);
							///////////////////

						}
						messageAlert(msg);
					}else{
						//console.log(jsonarray);
					}
					zhl_upload_close();return;
				},'json');
			}
		};
})(jQuery);